$(function() {
	queryProductInfo(1);
	$("#toPage").blur(function(){
		var topage = $("#toPage").val();
		var r = /^[0-9]*[1-9][0-9]*$/;
		if (!r.test(topage)) {
			alert("请正确输入要跳转的页面");
			$("#toPage").val("");
		} else {
			queryProductInfo(topage,"topage");
		}
	});
});

function queryProductInfo(pageNo,flag){
	if(pageNo == 0){
		pageNo = parseInt($("#pageNo").val());
		if(flag == "prev"){
			pageNo = pageNo-1;
			if(pageNo < 1){
				pageNo = 1;
			}
		} else if(flag == "next"){
			pageNo = pageNo+1;
		} else if (flag == "first"){
			pageNo = 1;
		} else if (flag == "end"){
			pageNo = parseInt($("#totalPage").val());
		}
	} 
	$("#pageNo").val(pageNo);
	var htm = "";
	var pageInfo = "当前第 1 页，共 1 页, 0 条记录";
	$.post("/product/productList", $("#productForm").serialize(), function(result) {
		if(result.code==200){
			var list = result.data;
			if(list.length == 0){
				htm = "<span style='color:red'>无记录......</span>";
			}else{
				var page = result.extraData.page;
				for(var i =0;i<list.length;i++){
					htm += "<tr style='text-align:center'>";
					htm += "<td>"+ page.offset++ +"</td>";
					htm += "<td valign='middle'>"+list[i].productName+"</td>";
					htm += "<td>"+list[i].developer+"</td>";
					htm += "<td>"+list[i].productManager+"</td>";
					htm += "<td>"+list[i].operationsStaff+"</td>";
					var productDesc = list[i].productDesc==null ? '':list[i].productDesc;
					if(productDesc.length<=13){
						htm += "<td>"+productDesc+"</td>";
					}else{
						htm += "<td>"+productDesc.substring(0,13)+"......</td>";
					}
					htm += "<td><a href='/product/gotoUpdateProduct?id="+list[i].id+"'>查看</a>" +
							"<a style='margin-left:10px;' href='javascript:deleteProduct("+list[i].id+")'>删除</a></td>";
					htm += "</tr>";
				}
				var page = result.extraData.page;
				$("#totalPage").val(page.totalPage);
				if(pageNo >= page.totalPage){
					pageNo = page.totalPage;
				}
				pageInfo = "当前第 "+page.currentPage+" 页，共 "+page.totalPage+" 页, "+page.totalResult+" 条记录";
			}
		}else{
			htm = "系统异常";
		}
		$("#pageNo").val(pageNo);
		$("#tableBody").html(htm);
		$("#pageInfo").html(pageInfo);
	});
}

function deleteProduct(productId) {
	var msg="确定删除吗";
	if (confirm(msg)) {
		$.post("/product/deleteProduct", {id:productId}, function(result) {
			if(result.code==200){
				window.location.reload();
			}else{
				alert("删除失败,系统异常");
			}
		});
	} else {
		return false;
	}
}
